<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;
class HActionController extends HomebaseController {
	function topcity(){
		$city = M("Constant")->where("parentid = 2")->select();
		$this->success($city);
	}
	
	function subcity(){
		$id = $_POST['id'];
		$city = M("Constant")->where("parentid=$id")->select();
		$this->success($city);
	}
	
	function toptype(){
		$type = M("Constant")->where("parentid = 3")->select();
		$this->success($type);
	}
	
	function subtype(){
		$id = $_POST['id'];
		$city = M("Constant")->where("parentid=$id")->select();
		$this->success($city);
	}
	
	function itemselc(){
		$type = $_POST['type'];
		$topcity = $_POST['topcity'];
		$subcity = $_POST['subcity'];
		$order = $_POST['order'];
		$page = $_POST['page'];
		///类型
		if($type=="*") $sql_type = "";
		else $sql_type = " and a.type_id = $type ";
		///城市
		if($topcity=="*")$sql_city = "";
		else if($subcity=="*")$sql_city=" and a.province_id =$topcity ";
		else $sql_city=" and a.province_id =$topcity and a.city_id=$subcity ";
		///排序
		if($order=="*")$sql_or = "";
		else $sql_or = " order by a.".$order;
			
		$page = " LIMIT ".(($page-1)*10).",100";
		$page="";
		if(strlen($sql_city)==0&&strlen($sql_type)==0)
			$sql = "select a.*,b.*,c.icon,b.id as st_id from ( m_ad as a right join m_stand as b on b.id = a.stand_id) left join m_img_lib as c ".
			"on c.ad_id =a.id and c.is_first=2 GROUP BY st_id ".$sql_or.$page;
		else{
			$sql = "select a.*,b.*,c.icon,b.id as st_id from ( m_ad as a right join m_stand as b on b.id = a.stand_id ".$sql_type.$sql_city.") left join m_img_lib as c ".
			"on c.ad_id =a.id and c.is_first=2 GROUP BY st_id ".$sql_or.$page;
		}
//		$this->success($sql);
//		return;
		$res = M()->query($sql);
		$now = date('Y-m-d');
		for($i=0,$len = count($res);$i<$len;++$i){
			if($res[$i]['expire_date']<$now)$res[$i]['st_id']=null;
		}
		$this->success($res);
	}

  function itemselc_kind(){
    $type = $_POST['type'];
    $topcity = $_POST['topcity'];
    $subcity = $_POST['subcity'];
    $order = $_POST['order'];
    $page = $_POST['page'];
    ///类型
    if($type=="*") $sql_type = "1=1";
    else $sql_type = "a.type = $type ";
    ///城市
    if($topcity=="*")$sql_city = "";
    else if($subcity=="*")$sql_city=" and a.province_id =$topcity ";
    else $sql_city=" and a.province_id =$topcity and a.city_id=$subcity ";
    ///排序
    if($order=="*")$sql_or = "";
    else $sql_or = " order by a.".$order;
      
    $page = " LIMIT ".(($page-1)*10).",100";
    $page="";
    if(strlen($sql_city)==0&&strlen($sql_type)==0)
      $sql = "select a.*,b.icon,c.pvalue from m_publish a left join m_img_lib as b ".
      "on b.publish_id =a.id and b.is_first=2 join m_constant as c on c.id = a.type where a.status=3 and $sql_type ".$sql_or.$page; 
    else{
//    $sql = "select a.*,b.*,c.icon,b.id as st_id from ( m_ad as a right join m_stand as b on b.id = a.stand_id ".$sql_type.$sql_city.") left join m_img_lib as c ".
//    "on c.ad_id =a.id and c.is_first=2 GROUP BY st_id ".$sql_or.$page;
      $sql = "select a.*,b.icon,c.pvalue from m_publish a left join m_img_lib as b ".
        "on b.publish_id =a.id and b.is_first=2 join m_constant as c on c.id = a.type where a.status=3 and $sql_type ".$sql_or.$page;
    }
//    $this->success($sql);
//    return;
    $res = M()->query($sql);
    for($i=0,$len=count($res);$i<$len;++$i){
      $res[$i]['create_time'] = formatTime($res[$i]['create_time']);
    }
    $now = date('Y-m-d');
    $this->success($res);
  }

	public function upimg(){
		$modify = $_POST['modify'];
		$index = $_POST['index'];
		$url = $_POST['url'];
    $type = i('type',null);
    if(!empty($type)){
      $data = session("kindgridimgs");
      $data[$index] = $url;
      if($modify==1)session("kindmodifyimgs",$data);
      else {
        session("kindgridimgs",$data);
      }  
    }else{
      $data = session("gridimgs");
      $data[$index] = $url;
  		if($modify==1)session("modifyimgs",$data);
  		else {
  		  session("gridimgs",$data);
      }
    }
		$this->success(1);
	}
	
	public function order_post(){
		$user=sp_get_current_user();
		$roll = $_GET['roll'];
		$Ad = M("Ad");
		$data=$Ad->create();
		$modify = $_POST['modify'];
//		$data['status'] = 1;
		$Imglis = M("ImgLib");
		if($modify==1||$modify=="1"){
			$data['modify'] = date("Y-m-d H:i:s");
			$modifyimgs = session("modifyimgs");
			$libs =M("Ad")->where("user_uniq_id = '".$data['user_uniq_id']."'")->select();
			$aid = $libs[$i]['id'];
			for($i =0,$len = count($modifyimgs);$i<$len;++$i){
				$Imglis->where("ad_id = $aid")->setField("icon = ".$modifyimgs[$i]['icon']);
			}
			$Ad->where("user_uniq_id = '".$data['user_uniq_id']."' and stand_id = ".$data['stand_id'])->data($data)->save();
			redirect("/HUser/subscribe");
			return;
		}else {
			$data['create_time'] = date("Y-m-d H:i:s");
			$begint = strtotime($data['create_time']);
			$endt = strtotime($data['expire_date']);
			$dift = ($endt - $begint)/86400;
			if($user['balance']<$data['spend']){
				session("needpay",$data['spend']-$user['balance']);
				session("payeddata",$data);
				redirect("/HUser/recharge");
				exit;
			}
			$stand_id = $data['stand_id'];
			$standhav = $Ad->where("stand_id = $stand_id")->count();
			if($standhav>=1){
				session("griderror","抱歉 该展位已锁定");
				redirect("/HGrid/grid");
				exit;
			}
			/*test*/
//			$resmess = M("Constant")->where("id = 25")->select();
//			$mess = explode(",",$resmess[0]['pvalue']);
//			for($i =0,$len=count($mess);$i<$len;$i++){
//				var_dump($mess[$i]);
//			}
//			return;
			/*test*/
			$user['balance'] = $user['balance']-$data['spend'];
			session("user",$user);
			M("AppUser")->where("user_uniq_id =".$user['user_uniq_id'])->data($user)->save();
			
			$sql=$Ad->add($data);
			
			$adid = M()->getLastInsID();
			$Stand = M("Stand");
			$Stand->where("id = $stand_id")->setField("ad_id",$adid);
			$imgs = session("gridimgs");
			for($i =0;$i<count($imgs);++$i){
				$il['ad_id'] = $adid;
				$il['is_first'] = $i==0?2:1;
				$il['icon'] = $imgs[$i];
				$Imglis->add($il);
			}
			$resmess = M("Constant")->where("id = 25")->select();
			$mess = explode(",",$resmess[0]['pvalue']);
			for($i =0,$len=count($mess);$i<$len;$i++){
				xw_send_code($mess[$i],'您有新的广告审核，请登录查看http://'.$_SERVER['HTTP_HOST'].'/Admin/');
			}
			redirect("/HUser/subscribe");
		}
		
	}

  public function kindorder_post(){
    $user=sp_get_current_user();
    $roll = $_GET['roll'];
    $id= i("id");
    $Publish = M("Publish");
    $data=$Publish->create();
    $modify = $_POST['modify'];
//    $data['status'] = 1;
    $Imglis = M("ImgLib");
    if($modify==1||$modify=="1"){
      $data['modify'] = date("Y-m-d H:i:s");
      $modifyimgs = session("kindmodifyimgs");
      $libs =$Publish->where("user_uniq_id = '".$data['user_uniq_id']."'")->select();
      for($i =0,$len = count($modifyimgs);$i<$len;++$i){
        $Imglis->where("publish_id = $id")->setField("icon = ".$modifyimgs[$i]['icon']);
      }
      $Publish->where("id = $id")->save($data);
      redirect("/HUser/subscribe");
      return;
    }else {
      $data['create_time'] = date("Y-m-d H:i:s");
      $data['user_uniq_id'] = $user['user_uniq_id'];
      $adid = $Publish->add($data);
      /*test*/
//      $resmess = M("Constant")->where("id = 25")->select();
//      $mess = explode(",",$resmess[0]['pvalue']);
//      for($i =0,$len=count($mess);$i<$len;$i++){
//        var_dump($mess[$i]);
//      }
//      return;
      /*test*/
      $imgs = session("kindgridimgs");
      for($i =0;$i<count($imgs);++$i){
        $il['publish_id'] = $adid;
        $il['is_first'] = $i==0?2:1;
        $il['icon'] = $imgs[$i];
        $Imglis->add($il);
      }
      $resmess = M("Constant")->where("id = 25")->select();
      $mess = explode(",",$resmess[0]['pvalue']);
      for($i =0,$len=count($mess);$i<$len;$i++){
        xw_send_code($mess[$i],'您有新的广告审核，请登录查看http://'.$_SERVER['HTTP_HOST'].'/Admin/');
      }
      redirect("/HUser/subscribe");
    }
    
  }
	
	public function update(){
		$this->success("已是最新版本");
	}

	public function exchange(){
		$gradeid = $_POST['id'];
		$user=sp_get_current_user();
		if(!isset($gradeid)){
			$this->error("获取参数不正确");
			return;
		}
		$Const = M("Constant");
		$cgrade = $Const->where("id = $gradeid")->select();
		$now = $Const->where("pname = '".$user['grade']."'")->select();
		if($user['grade']==$cgrade[0]['pname']){
			$this->error("已为当前等级 无需再次兑换");
			return;
		}
		else if($user['balance']<$cgrade[0]['pvalue']){
			$this->error("余额不足 无法兑换");
			return;
		}else if($now[0]['listorder']>$cgrade[0]['listorder']){
			$this->error("高于兑换等级 无需兑换");
		}else {
			$data['grade'] = $cgrade[0]['pname'];
			$data['picon'] = $cgrade[0]['picon'];
			$data['balance'] = $user['balance']- $cgrade[0]['pvalue'];
			$res = M("AppUser")->where("user_uniq_id=".$user['user_uniq_id'])->data($data)->save();
			if($res){
				session("user",M("AppUser")->where("user_uniq_id =".$user['user_uniq_id'])->find());
				$this->success("兑换成功!");
			}else{
				$this->success("兑换失败!");
			} 
		}
	} 
	function isvisible(){
		$val = i('val');
		$Appu = M("AppUser");
		$data=sp_get_current_user();
		$res = M()->execute("update m_app_user set is_visible =".$val." where user_uniq_id = ".$data['user_uniq_id']);
		session("user",$Appu->where("user_uniq_id =".$data['user_uniq_id'])->find());
		if($res)$this->success($res);
		else $this->error($res);
	}
	
	function resetpwd(){
		$se_user = session('user_account');
		$se_code = session('message_code');
		$user = I("user_account","");
		$code = I("message_code","");
		$pwd = I("password","");
		$c_pwd = I("c_password","");
		if(empty($user)||strlen($user)!=11){
			$this->error("手机号格式不正确");return;
		}
		if(empty($code)||strlen($code)==0){
			$this->error("未填写验证码");return;
		}
		if(empty($code)||strlen($code)==0){
			$this->error("未填写验证码");return;
		}
		if(empty($pwd)){
			$this->error("未填写密码");return;
		}
		if($pwd!=$c_pwd){
			$this->error("两次密码不一致");return;
		}
		if($code!=$se_code){
			$this->error("验证码错误");return;
		}
		if($se_user!=$user){
			$this->error("手机号错误");return;
		}
		$res = M("AppUser")->where("user_account = '$user'")->setField('password',$pwd);
		if($res)$this->success("重置密码成功");
		else $this->error("重置密码失败");
	}
	
}